package com.yanshaohui.springboot.mapper

import org.apache.ibatis.jdbc.SQL
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import com.yanshaohui.springboot.entity.AddressEntity

class AddressDynaSqlProvider {

	String selectAddress(final AddressEntity entity) {
		new SQL() {
			{
				SELECT("*");
				FROM("ADDRESS");
				if(entity.getName() != null){
					WHERE("name = #{name}");	
				}
				if (entity.getAddress() != null) {
					WHERE("ADDRESS like '%'||#{address}||'%'");
				}
				if (entity.getPost() != null) {
					WHERE("POST = #{post}");
				}
				if (entity.getTel() != null) {
					WHERE("TEL = #{tel}");
				}
			}
		}.toString();
	}
	
	String insertAddress(final AddressEntity entity) {
		new SQL() {
			{
				INSERT_INTO("ADDRESS");
				if (entity.getName() != null) {
					VALUES("NAME", "#{name}");
				}
				if (entity.getAddress() != null) {
					VALUES("ADDRESS", "#{address}");
				}
				if (entity.getPost() != null) {
					VALUES("POST", "#{post}");
				}
				if (entity.getTel() != null) {
					VALUES("TEL", "#{tel}");
				}
			}
		}.toString();
	}

	String updateAddress(final AddressEntity entity) {
		new SQL() {
			{
				UPDATE("ADDRESS");
				if (entity.getName() != null) {
					SET("NAME=#{name}");
				}
				if (entity.getAddress() != null) {
					SET("ADDRESS=#{address}");
				}
				if (entity.getPost() != null) {
					SET("POST=#{post}");
				}
				if (entity.getTel() != null) {
					SET("TEL=#{tel}");
				}
				WHERE("id=#{id}");
			}
		}.toString();
	}
	
}
